//
//  LXAllCarListViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/1/10.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXAllCarListViewController.h"
#import "CheLiangListCell.h"
#import "CheLiangDetailViewController.h"
#import "LXCarLeaseApplyViewController.h"
#import "LXSellApplyViewController.h"
#import "LXCarAddViewController.h"
@interface LXAllCarListViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate,UITextFieldDelegate>

@property (nonatomic,strong)  UITableView       *  tableView;
@property (nonatomic, strong) NSMutableArray    *  dataAry;
@property (nonatomic, assign) NSInteger            pageIndex;
@property(nonatomic) NSString   *searchStr;
@property(nonatomic) LXTopSearchView *searchView;
@end

@implementation LXAllCarListViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self layoutCustomNav:self.view title:@"全部车辆"];
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
   
    
}

- (void)viewDidLoad {
     [super viewDidLoad];
    
    self.view.backgroundColor = ColorWithF5F5F5;
    
    LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0,LL_StatusBarAndNavigationBarHeight, kScreenWidth, 70)];
    [self.view addSubview:searchView];
    self.searchView = searchView;
    searchView.textField.placeholder = @"输入车牌号查询";
    // 点击
    [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
        [self.view endEditing:YES];
        self.searchStr = text;
        [self.tableView.mj_header beginRefreshing];
    }];
    
    
    
    [self.view addSubview:self.tableView];
   
   BD_WEAKSELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       weakSelf.pageIndex = 1;
       [self loadData];
   }];

    [self.tableView.mj_header beginRefreshing];
   self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
       self.pageIndex++;
       [self loadData];
   }];
    
    if (self.isShowAdd) {
        [self setBottomViewWithText:@"新增车辆"];
        self.tableView.height = kScreenHeight-MaxY(self.searchView) - kComHeight;
    }
    
    
    
}
-(void)loadData{

    NSMutableDictionary *dic = @{
        @"page":@(self.pageIndex),
        @"rows":@(10),
        @"signType":@(2), // 全部
    }.mutableCopy;
        
    if (!StringIsEmpty(self.searchStr)) {
        [dic setValue:self.searchStr forKey:@"carNum"];
    }
   
    [MSNetworkManager postRevertUrl:CarList_URL params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        if (isSuccess) {
            
            if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
                [self.dataAry removeAllObjects];
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
            NSInteger totalPage = [resonseObj[@"total"] intValue];
            
            NSArray *cacheArray = resonseObj[@"list"];
            [self.dataAry addObjectsFromArray:[CheLiangModel mj_objectArrayWithKeyValuesArray:cacheArray]];
            
            if (self.dataAry.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
            [self.tableView reloadData];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
    } withShowHud:YES];
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CheLiangListCell *cell = [tableView dequeueReusableCellWithIdentifier:[CheLiangListCell reuseIdentifier]];
    if (!cell) {
        cell = [[CheLiangListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CheLiangListCell reuseIdentifier]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CheLiangModel *model = self.dataAry[indexPath.row];
    cell.allModel = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CheLiangModel  *model = self.dataAry[indexPath.row];
    CheLiangDetailViewController *vc = [[CheLiangDetailViewController alloc] init];
    vc.detailModel = model;
    [self pushToNextController:vc];
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, MaxY(self.searchView), kScreenWidth, kScreenHeight-MaxY(self.searchView))];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.rowHeight = 120;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.backgroundColor = ColorWithF5F5F5;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       [_tableView registerNib:[UINib nibWithNibName:[CheLiangListCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[CheLiangListCell reuseIdentifier]];
    }
    return _tableView;
}

- (NSMutableArray *)dataAry {
    if (!_dataAry) {
        self.dataAry = [NSMutableArray array];
    }
    return _dataAry;
}
#pragma mark - ------------------ DZNEmptyData ------------------
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"No_order_icon"];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (!self.tableView.mj_header.isRefreshing && !self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer resetNoMoreData];
         self.pageIndex = 1;
        [self loadData];
    }
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -50;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}

- (void)clickBottomView:(UIButton *)sender{
    
    LXCarAddViewController *vc = [[LXCarAddViewController alloc] init];
    [self pushToNextController:vc];
    
}

@end
